from app import app, db
from models.models import User, BankCard, Transaction, UserAuth, DepositBusiness

def create_all_tables():
    with app.app_context():
        # 删除所有表（如果存在）
        db.drop_all()
        print("已删除所有现有表")
        
        # 创建所有表
        db.create_all()
        print("已创建所有表")
        
        # 插入测试数据
        # 1. 创建测试用户
        user1 = User(name='张三', id_card='110101199001011234', phone='13800138001')
        user2 = User(name='李四', id_card='110101199002022345', phone='13800138002')
        db.session.add_all([user1, user2])
        db.session.commit()
        print("已创建测试用户")
        
        # 2. 创建存款业务类型
        businesses = [
            DepositBusiness(
                business_name='活期',
                description='无固定存期，可随时存取，存取金额不限的一种比较灵活的存款',
                min_amount=0.0,
                max_amount=None,
                interest_rate=0.35,
                term_options='',
                is_active=1
            ),
            DepositBusiness(
                business_name='定活两便',
                description='事先不约定存期，一次性存入，一次性支取的存款',
                min_amount=50.0,
                max_amount=None,
                interest_rate=1.0,
                term_options='',
                is_active=1
            ),
            DepositBusiness(
                business_name='整存整取',
                description='选择存款期限，整笔存入，到期提取本息，是一种定期储蓄。银行提供的存款期限有1年、2年和3年',
                min_amount=1000.0,
                max_amount=None,
                interest_rate=2.75,
                term_options='1年,2年,3年',
                is_active=1
            ),
            DepositBusiness(
                business_name='零存整取',
                description='一种事先原定金额，逐月按约定金额存入，到期支取本息的定期储蓄。银行提供的存款期限有1年、2年和3年',
                min_amount=50.0,
                max_amount=None,
                interest_rate=1.35,
                term_options='1年,2年,3年',
                is_active=1
            )
        ]
        db.session.add_all(businesses)
        db.session.commit()
        print("已创建存款业务类型")
        
        # 3. 创建测试银行卡
        card1 = BankCard(card_number='6222021234567890123', balance=10000.0, user_id=user1.id, deposit_business_id=1)
        card2 = BankCard(card_number='6222021234567890124', balance=5000.0, user_id=user1.id, deposit_business_id=3)
        card3 = BankCard(card_number='6222021234567890125', balance=8000.0, user_id=user2.id, deposit_business_id=2)
        db.session.add_all([card1, card2, card3])
        db.session.commit()
        print("已创建测试银行卡")
        
        # 4. 创建测试交易记录
        transactions = [
            Transaction(card_id=card1.id, type='deposit', amount=5000.0),
            Transaction(card_id=card1.id, type='withdraw', amount=1000.0),
            Transaction(card_id=card2.id, type='deposit', amount=3000.0),
            Transaction(card_id=card3.id, type='deposit', amount=8000.0),
        ]
        db.session.add_all(transactions)
        db.session.commit()
        print("已创建测试交易记录")
        
        # 5. 创建用户认证账号
        auth1 = UserAuth(username='admin1', password='adminpass', role='admin')
        auth2 = UserAuth(username='customer1', password='custpass', role='customer', user_id=user1.id)
        auth3 = UserAuth(username='13800138001', password='123456', role='customer', user_id=user1.id)
        auth4 = UserAuth(username='13800138002', password='234567', role='customer', user_id=user2.id)
        db.session.add_all([auth1, auth2, auth3, auth4])
        db.session.commit()
        print("已创建用户认证账号")
        
        print("\n数据库初始化完成！")
        print("测试账号：")
        print("- 管理员：admin1 / adminpass")
        print("- 客户1：customer1 / custpass")
        print("- 客户1：13800138001 / 123456")
        print("- 客户2：13800138002 / 234567")

if __name__ == '__main__':
    create_all_tables() 